package com.mallcai.bigdata.ladon.dpl.mapper.peekdata.provider;

import com.mallcai.bigdata.ladon.dpl.entity.peekdata.ImportRecord;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import com.mallcai.bigdata.ladon.dpl.mapper.system.provider.UserSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-12 16:32
 */
public class ImportRecordSqlProvider extends BaseSqlProvider<ImportRecord> {

    public static final String TB_IMPORT_RECORD = "pd_import_record";

    @Override
    public String getTableName() {
        return TB_IMPORT_RECORD;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("file_name", "#{fileName}");
        map.put("table_name", "#{tableName}");
        map.put("datasource_id", "#{datasourceId}");
        map.put("datasource_type", "#{datasourceType}");
        map.put("overwrited", "#{overwrited}");
        map.put("partition_name", "#{partitionName}");
        map.put("archive", "0");
        map.put("status", "0");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        return null;
    }


    /**
     * 根据参数查询
     */
    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("tb_p.archive = 0");
        if (params.containsKey("fileName") && StringUtils.isNotBlank(params.get("fileName").toString())) {
            sb.append(" and tb_p.file_name like concat('%', #{params.fileName}, '%')");
        }

        if (params.containsKey("createdBy")) {
            sb.append(" and tb_p.created_by = #{params.createdBy}");
        }
        return new SQL() {
            {
                SELECT("tb_p.*, tb_m.name AS datasource_name, tb_u.nickname AS creator");
                LEFT_OUTER_JOIN("c_datasource tb_m  ON tb_m.id = tb_p.datasource_id ");
                LEFT_OUTER_JOIN(UserSqlProvider.TB_USER + " tb_u ON tb_u.id = tb_p.created_by ");
                FROM(TB_IMPORT_RECORD + " tb_p");
                WHERE(sb.toString());
                ORDER_BY("tb_p.created DESC");
            }
        }.toString();
    }
}
